defmodule PangoMqttHandler do
	@moduledoc false
	use Tortoise.Handler
	require Logger

	@impl true
	def init([pid, mod]) do
		{:ok, %{"pid" => pid, "mod" => mod}}
	end

	@impl true
	def subscription(status, topic_filter, state) do
		Logger.debug(
			"subscription status: #{inspect(status)}, topic filter: #{inspect(topic_filter)}"
		)

		case status do
			:up ->
				nil

			:down ->
				nil
		end

		{:ok, state}
	end

	@impl true
	def terminate(reason, %{"pid" => pid, "mod" => mod}) do
		Logger.error(reason)
		mod.connection_terminated(pid)
		:ignored
	end

	@impl true
	def connection(status, %{"pid" => pid, "mod" => mod} = state) do
		case status do
			:up ->
				nil
			:down ->
				nil
			:terminated ->
				mod.connection_terminated(pid)
			_ ->
				nil
		end

		{:ok, state}
	end

	@impl true
	def handle_message(_topic_levels, _payload, %{} = state) do
		{:ok, state}
	end


end
